<template>
<div>
  <div class="todo-container">
    <div class="todo-wrap">
      <Header :add="add"></Header>
      <List
          :todoList="todoList"
          :deleteId="deleteId"
          :changeDoneId="changeDoneId"
      >
      </List>
      <Footer
          :deleteDoneAll="deleteDoneAll"
          :todoList="todoList"
          :changeAllDone="changeAllDone"
      >
      </Footer>
    </div>
  </div>
</div>
</template>


<script>
import Header from "@/components/Header";
import List from "@/components/List";
import Footer from "@/components/Footer";
export default {
    name:"App",
  components: {Footer, List, Header},
  data(){
      return{
        todoList:[]
      }
  },
  methods:{
      //添加数据
      add(e){
        const content = e.target.value;
        if(content.length === 0){
          alert("请输入内容")
          return;
        }
        if(this.todoList.find(v=>v.content === content)){
          alert("请勿输入重复内容")
          return;
        }
        const todoObj = {id:Math.random().toString(36).slice(2),content,done:false}
        this.todoList.unshift(todoObj)
        e.target.value = null
      },
      //删除单个数据
      deleteId(id){
         this.todoList = this.todoList.filter(v=>v.id !== id)
      },
      //根据id选中状态
    changeDoneId(id) {
      this.todoList.map(v=>{
        if(v.id === id){
          v.done = !v.done
        }
        return v
      })
    },
      //清除已完成
    deleteDoneAll(){
        this.todoList =  this.todoList.filter(v=> !v.done)
    },
    changeAllDone(newVal){
        this.todoList.map(v=>{
          v.done = newVal
          return v
        })
    }
  },
  watch:{
      todoList:{
          deep:true,
          handler(){
            localStorage.setItem("todoList",JSON.stringify(this.todoList))
          }
      }
  },
  mounted() {
      this.todoList = JSON.parse(localStorage.getItem("todoList")||"[]")
  }
}
</script>


<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}


/*app*/
.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}




/*item*/
li {
  list-style: none;
  height: 36px;
  line-height: 36px;
  padding: 0 5px;
  border-bottom: 1px solid #ddd;
}

li label {
  float: left;
  cursor: pointer;
}

li label li input {
  vertical-align: middle;
  margin-right: 6px;
  position: relative;
  top: -1px;
}

li button {
  float: right;
  display: none;
  margin-top: 3px;
}

li:before {
  content: initial;
}

li:last-child {
  border-bottom: none;
}
li .done {
  color: #666;
  text-decoration: line-through;
}

/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}

</style>